@php
    $user = Auth::user();
    $roleType = $user->cur_role_type;
@endphp

@extends('layouts.default')

@section('title', '课题研究')

@push('scripts')
<script>
    var filters = {};
    var per_page = 10;
    var totalNum = 0;
    layui.use(['layer','form','table','laytpl','laydate','element','laypage'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var element = layui.element;
        var laypage = layui.laypage;
        var materials_date = "{{request()->get('materials_date',0)}}";
        var user_id = "{{request()->get('user_id',0)}}";
        
        renderSelect();

        laydate.render({
            elem: '#range-date'
            ,type: 'date'
            ,range: true
        });

        // 搜索查询
        form.on('submit(queryBtn)', function(data) {
            var param = data.field;
            filters = param
            getList(1, per_page);
        });
        // 重置
        form.on('submit(resetBtn)', function(data) {
            filters = {};
            $('#qryForm')[0].reset();
            getList(1, per_page)
        });
        // 获取页面数据
        function getList(page, limit) {
            if ($.isEmptyObject(filters)){
                let param = form.val("qryForm");
                filters = param;
            }
            
            var para = {
                "page": page,
                "search": filters,
                "limit": limit,
                "form_key": "TEARRESEARCH_SUBJECT_STUDY_TABLE",
                "materials_date": materials_date,
                "user_id": user_id,
            };

            getKtyjList(para, function(res){
                if(res.code === 1) {
                    if(totalNum != res.total) {
                        totalNum = res.total;
                        per_page = res.limit;
                        renderPageBar(page,per_page)
                    }

                    table.render({
                        data: res.data,
                        elem: '#tablelsit',
                        toolbar: '#toolbarHead', //开启头部工具栏，并为其绑定左侧模板
                        defaultToolbar: [],
                        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        limit: res.limit,
                        skin: 'nob',
                        cols: [[
                            {field: 'term_name', title: '学年学期', align: 'left', templet: function(d) {
                                let termName = d.data_version_id ? d.term.title : '-';
                                return d.data_version_name ? d.data_version_name : termName;
                            }},
                            {field: 'name', title: '课题名称', align: 'center'},
                            {field: 'activity_type', title: '类别', align: 'center', templet: function(d) {
                                if(d.activity_category == 'EW_SUBJECT_RESEARCH_TABLE') {
                                    return '教育';
                                } else if(d.activity_category == 'TEARRESEARCH_SUBJECT_STUDY_TABLE') {
                                    return '教学'
                                } else {
                                    return '-';
                                }
                                return d.level_array.length > 0 ? d.level_array[0].selectValue : '-';
                            }},
                            {field: 'begin_date', title: '开始日期', align: 'center'},
                            {field: 'end_date', title: '结束日期', align: 'center'},
                            {field: 'level_name', title: '级别', align: 'center', templet: function(d) {
                                return d.level_array.length > 0 ? d.level_array[0].selectValue : '-';
                            }},
                            {field: 'compere_name', title: '主持人', align: 'center', templet: function(d) {
                                return d.compere.length > 0 && d.compere[0].user ? d.compere[0].user.nickname : '-';
                            }},
                            {field: 'creator', title: '创建人', align: 'center', templet: function(d) {
                                return d.creator ? d.creator.nickname : '-';
                            }},
                            {field: 'stage_name', title: '课题阶段', align: 'center', templet: function(d) {
                                switch(d.stage) {
                                    case 1:
                                        return '申报阶段';
                                        break;
                                    case 2:
                                        return '立项阶段';
                                        break;
                                    case 3:
                                        return '中期阶段';
                                        break;
                                    case 4:
                                        return '结项阶段';
                                        break;
                                    default:
                                        return '-';
                                        break;
                                }
                            }},
                            {field: 'status_name', title: '状态', align: 'center', templet: function(d) {
                                if(d.stage === 0 && d.old_status) {
                                    switch(d.old_status) {
                                        case 'REVOKE':
                                            return '撤回';
                                            break;
                                        case 'FILL_IN':
                                            return '未提交';
                                            break;
                                        case 'REVIEW':
                                            return '待审核';
                                            break;
                                        case 'REVIEW_FAIL':
                                            return '已驳回';
                                            break;
                                        case 'PASS':
                                            return '审核通过';
                                            break;
                                        default:
                                            return '-';
                                            break;
                                    }
                                }
                                
                                let statusName = "";
                                if(d.stages.length > 0) {
                                    let Stages = d.stages.length > 0 ? d.stages : [];
                                    layui.each(Stages, function(i, s) {
                                        if(s.stage == d.stage) {
                                            statusName = s.status_set;
                                        }
                                    });

                                    if(!statusName) {
                                        statusName = "待提交";
                                    }
                                } else {
                                    statusName = "-";
                                }

                                return statusName;
                            }},
                            {title: '操作', width: '22%', align: 'center', toolbar: '#opDemo', fixed:'right'}
                        ]],
                        id:"tableIns",
                        done: function(res, curr, count) {
                            setBtnPermission();
                            curr_page = page;
                        },
                        page: false,
                        loading: true
                    });
                } else {
                    layer.msg(res.msg, {time: 2000, icon: 5});
                }
            });
        }

        // 渲染页面
        function renderPageBar(page,limit) {
            laypage.render({
                elem: 'pageBar',
                count: totalNum, //数据总数，从服务端得到
                curr: page,
                first: '第一页',
                last: '最后一页',
                limit:limit,
                layout: ['limit','count', 'prev', 'page', 'next', 'skip'],
                jump: function(obj, first) {
                    //首次不执行
                    if(!first) {
                        layer.closeAll();
                        getList(obj.curr,obj.limit)
                    }
                }
            });
        }

        //表格头工具栏事件
        table.on('toolbar(tablelsit)', function(obj){
            switch(obj.event){
                case 'add':
                    
                break;
            };
        });
        //监听行工具事件
        table.on('tool(tablelsit)', function(obj){
            var data = obj.data;

            if(obj.event === 'del'){
                layer.confirm('确定删除吗?', function(index){
                    delActivity({id: data.id, activity_category: data.activity_category}, function(res) {
                        if (res.code == '9') {
                            layer.msg(res.msg, {icon: 5, shift: 6});
                        } else {
                            layer.msg('删除成功', {icon: 6,time: 1000},function(){
                                obj.del();
                                layer.close(index);
                            });
                        }
                    });
                });
            }
        });


        //下拉框初始化
        async function renderSelect(){
            //await renderDictionarySelect();
            //await renderTermSelect();
            getList(1, per_page);
        }

        //字典下拉框数据渲染
        function renderDictionarySelect() {
            return new Promise((resolve) => {
                getDictionaryItem({'diccode': ['LEVEL', 'CLASSITEM']}, function (res){
                    if(res.code === 1) {  
                        let data = res.data;

                        for(var k in data) {
                            let key = k.toLowerCase();
                            let d = data[k];
                            
                            $('[data-role='+key+'List]').empty().append('<option value="">请选择</option>');
                            for (var i in d) {
                                $('[data-role='+key+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                            }
                        }
                        
                        form.render(); 
                        
                        resolve();
                    }
                });
            });
        }
        //学期下拉框数据渲染
        function renderTermSelect() {
            return new Promise((resolve) => {
                getTermData({},function(res){
                    if(res.code == 1){
                        $('[data-role=termList]').empty().append('<option value="">请选择</option>');
                        for(var i in res.data){
                            $('[data-role=termList]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        }
                                      
                        form.render('select');

                        resolve();
                    }
                });  
            });
        }
    });
</script>
@endpush

@section('content')
<style>
    
</style>



<div class="layui-card">
 
 
    <div class="layui-card-body">
        <table class="layui-hide" id="tablelsit" lay-filter="tablelsit" lay-data="{id: 'tablelsit'}"></table>
        <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
    </div>

    <script type="text/html" id="opDemo">
        <a btnPermission="224" class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" href="{{route('study.infoView')}}?id=@{{d.id}}">查看</a>
    </script>
</div>
  
@endsection
